/**
 * Created by cz on 2017/5/4.
 */
import React,{Component} from "react";
import {
    View,
    Text,
    Image,
    StyleSheet,
    ListView,
    TouchableOpacity,
    RefreshControl
}from "react-native";
var result = {
    "data": [
        {
            "email": "h.brown@young.gov",
            "thumb": "http://dummyimage.com/10*6/3eb280",
            "user": "张三张三张三张三"
        },
        {
            "email": "s.thompson@brown.net",
            "thumb": "http://dummyimage.com/10*6/b825a4",
            "user": "张三张三张三张三"
        },
        {
            "email": "c.martin@hernandez.io",
            "thumb": "http://dummyimage.com/10*6/ebbb02",
            "user": "张三张三张三"
        },
        {
            "email": "n.jackson@williams.co.uk",
            "thumb": "http://dummyimage.com/10*6/fbe889",
            "user": "张三张三张三"
        },
        {
            "email": "n.thomas@jones.gov",
            "thumb": "http://dummyimage.com/10*6/10a771",
            "user": "张三张三"
        },
        {
            "email": "i.clark@hernandez.edu",
            "thumb": "http://dummyimage.com/10*6/132de6",
            "user": "张三张三"
        },
        {
            "email": "t.anderson@thomas.org",
            "thumb": "http://dummyimage.com/10*6/0f830a",
            "user": "张三张三张三张三张三"
        },
        {
            "email": "u.rodriguez@jackson.org",
            "thumb": "http://dummyimage.com/10*6/c1923a",
            "user": "张三张三"
        },
        {
            "email": "h.moore@davis.org",
            "thumb": "http://dummyimage.com/10*6/9e835f",
            "user": "张三张三张三"
        },
        {
            "email": "x.lopez@rodriguez.com",
            "thumb": "http://dummyimage.com/10*6/89b278",
            "user": "张三张三"
        }
    ],
    "status": true
};
import Toast,{DURATION} from "react-native-easy-toast"
export default class ListViewTest extends Component{
    constructor(props){
        super(props);
        var ds = new ListView.DataSource({rowHasChanged:(r1,r2)=>r1 !== r2});
        this.state = {
            dataSource:ds.cloneWithRows(result.data),
            isLoading:true
        }
        this.onLoad();
    }
    renderRow(item){
        return <View style={styles.row}>
            <Image style={styles.logo} source={{uri:item.thumb}}></Image>
            <TouchableOpacity onPress={()=>{
                this.toast.show("你单机了"+item.user,DURATION.LENGTH_LONG);
            }}>
                <Text style={styles.tips}>{item.user}</Text>
                <Text style={styles.tips}>{item.email}</Text>
            </TouchableOpacity>
        </View>
    }
    renderSeparator(sectionID, rowID, adjacentRowHighlighted){
        return <View style={styles.line} key={rowID}></View>
    }
    renderFooter(){
        return <View><Text style={{textAlign:'center',fontSize:16}}>没有更多了...</Text></View>
    }
    onLoad(){
        setTimeout(()=>{
            this.setState({
                isLoading:false
            })
        })
    }
    render(){
        return (
            <View style={styles.container}>
                <ListView dataSource={this.state.dataSource}
                renderRow={(item)=>this.renderRow(item)}
                renderSeparator={(sectionID, rowID, adjacentRowHighlighted)=>this.renderSeparator(sectionID, rowID, adjacentRowHighlighted)}
                renderFooter={()=>this.renderFooter()}
                refreshControl={<RefreshControl
                    refreshing = {this.state.isLoading}
                    onFreshLoad={()=>this.onLoad()}
                    colors={["#dddddd"]}
                    tintColor={"#dddddd"}
                />}
                />
                <Toast ref={toast=>{this.toast=toast}}/>
            </View>
        )
    }
}
const styles = StyleSheet.create({
    container:{
        flex:1
    },
    tips:{
        fontSize:20
    },
    row:{
        padding:5
    },
    logo:{
        width:40,
        height:40
    },
    line:{
        height:1,
        backgroundColor:"#dddddd"
    }
});